package com.gph.saviorframework.security.web.editor;

import com.gph.saviorframework.common.model.Org;
import com.gph.saviorframework.security.service.OrgService;

import java.beans.PropertyEditorSupport;

/**
 * Created by savior on 2016/3/23.
 */
public class OrgEditor extends PropertyEditorSupport {

    private OrgService orgService;

    /**
     * @param orgService
     */
    public OrgEditor(OrgService orgService) {
        this.orgService = orgService;
    }

    /**
     * @return
     * @see PropertyEditorSupport#getAsText()
     */
    @Override
    public String getAsText() {
        if (getValue() instanceof Org) {
            return ((Org) getValue()).getId().toString();
        }
        return null;
    }

    /**
     * @param text
     * @throws IllegalArgumentException
     * @see PropertyEditorSupport#setAsText(String)
     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Object org = orgService.get(text);
        if (org != null) {
            setValue(org);
        } else {
            setValue(new Org(text));
        }
    }

}
